PROJECT(SZLIB)
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)

SET(SZLIB_LINK_TYPE SHARED)
SET(SZLIB_BUILD_TYPE DEBUG)
SET(CMAKE_INSTALL_PREFIX C:/opt/szlib)

# Check for needed headers
INCLUDE(${CMAKE_ROOT}/Modules/CheckIncludeFile.cmake)
CHECK_INCLUDE_FILE(dlfcn.h HAVE_DLFCN_H)
CHECK_INCLUDE_FILE(fcntl.h HAVE_FCNTL_H)
CHECK_INCLUDE_FILE(inttypes.h HAVE_INTTYPES_H)
CHECK_INCLUDE_FILE(memory.h HAVE_MEMORY_H)
CHECK_INCLUDE_FILE(stdint.h HAVE_STDINT_H)
CHECK_INCLUDE_FILE(stdlib.h HAVE_STDLIB_H)
CHECK_INCLUDE_FILE(strings.h HAVE_STRINGS_H)
CHECK_INCLUDE_FILE(string.h HAVE_STRING_H)
CHECK_INCLUDE_FILE(sys/stat.h HAVE_SYS_STAT_H)
CHECK_INCLUDE_FILE(sys/types.h HAVE_SYS_TYPES_H)
CHECK_INCLUDE_FILE(unistd.h HAVE_UNISTD_H)

# Check for needed functions
INCLUDE(${CMAKE_ROOT}/Modules/CheckFunctionExists.cmake)
CHECK_FUNCTION_EXISTS(memset HAVE_MEMSET)
CHECK_FUNCTION_EXISTS(vprintf HAVE_VPRINTF)
IF(NOT HAVE_VPRINTF)
  CHECK_FUNCTION_EXISTS(_doprnt HAVE_DOPRNT)
ENDIF(NOT HAVE_VPRINTF)

# Check for size_t
# Check size of various types..
INCLUDE(${CMAKE_ROOT}/Modules/CheckTypeSize.cmake)
CHECK_TYPE_SIZE(size_t size_t)


CONFIGURE_FILE(${PROJECT_SOURCE_DIR}/src/SZconfig.h.in ${PROJECT_SOURCE_DIR}/src/SZconfig.h)

# Source files
SET(SZLIB_SRCS
src/rice.h
src/ricehdf.h
src/SZconfig.h
src/szip_adpt.h
src/szlib.h
src/encoding.c
src/rice.c
src/sz_api.c
)

# Build Options
# Install prefix
OPTION(CMAKE_INSTALL_PREFIX
  "Install path prefix. Prepened to install directories," 
  C:/opt/szlib
  )

# Shared or static lbrary
OPTION(SZLIB_BUILD_SHARED
  "Build szlib as a shared library" 
  ON
)
# Set the build flag for static/shared
IF(SZLIB_BUILD_SHARED)    
  SET(SZLIB_LINK_TYPE SHARED)
ELSE(SZLIB_BUILD_SHARED)
  SET(SZLIB_LINK_TYPE STATIC)
ENDIF(SZLIB_BUILD_SHARED)    

# Add the target
SET(SZIP_LIBRARIES szlib PARENT_SCOPE)
ADD_LIBRARY(szlib ${SZLIB_LINK_TYPE} ${SZLIB_SRCS})

# Set the target properties
IF(SZLIB_BUILD_SHARED)    
  SET(SZLIB_COMPILE_DEFINITIONS _SZUSEDLL_)
  SET_TARGET_PROPERTIES(szlib PROPERTIES DEFINE_SYMBOL _SZDLL_)
  IF(WIN32)
    SET(SZLIB_VERSION 2.0)
  ELSE(WIN32)
    SET(SZLIB_VERSION 2.0.0)
  ENDIF(WIN32)
  SET_TARGET_PROPERTIES(szlib PROPERTIES VERSION ${SZLIB_VERSION})
  SET_TARGET_PROPERTIES(szlib PROPERTIES SOVERSION ${SZLIB_VERSION})
ENDIF(SZLIB_BUILD_SHARED)

# Debug or release build
OPTION(SZLIB_BUILD_DEBUG
  "Build szlib with debugging info"
  OFF
)
IF(SZLIB_BUILD_DEBUG)
  SET(SZLIB_BUILD_TYPE DEBUG)
ELSE(SZLIB_BUILD_DEBUG)
  SET(SZLIB_BUILD_TYPE RELEASE)
ENDIF(SZLIB_BUILD_DEBUG)

SET_PROPERTY(TARGET szlib APPEND PROPERTY COMPILE_DEFINITIONS_DEBUG DEBUG)
SET_PROPERTY(TARGET szlib APPEND PROPERTY COMPILE_DEFINITIONS_RELEASE NDEBUG)
#SET_TARGET_PROPERTIES(szlib PROPERTIES DEBUG_POSTFIX D)


OPTION(SZLIB_ENCODER_SUPPORT
  "Build szlib with encoder enabled"
  OFF
)
IF(SZLIB_ENCODER_SUPPORT)
  SET(HAVE_ENCODING)
ENDIF(SZLIB_ENCODER_SUPPORT)

# Deal with MS attempt to deprecate everything
IF(MSVC)
  SET_PROPERTY(TARGET szlib APPEND PROPERTY 
    COMPILE_DEFINITIONS _CRT_SECURE_NO_DEPRECATE)
  SET_PROPERTY(TARGET szlib APPEND PROPERTY 
    COMPILE_DEFINITIONS _CRT_NONSTDC_NO_DEPRECATE)
ENDIF(MSVC)

# Set up the include directories
SET(SZLIB_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/src PARENT_SCOPE)
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/src)

# Visual Studio Sugar
SET_TARGET_PROPERTIES(szlib PROPERTIES PROJECT_LABEL "szlib")

# Make Sugar
#SET(CMAKE_VERBOSE_MAKEFILE ON)

# Execute the build
TARGET_LINK_LIBRARIES(szlib)

SET(SZLIB_LIBRARIES szlib)